package org.duiduo.common.runtime.executor;

import org.duiduo.common.tool.util.ThreadMDCUtil;
import org.slf4j.MDC;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * 线程池配置
 *
 * @author liangze
 * @date 2021/12/29 11:33
 */
public class GlobalThreadPoolTaskExecutor extends ThreadPoolTaskExecutor implements DisposableBean {
    @Override
    public void execute(Runnable task) {
        super.execute(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    public void execute(Runnable task, long startTimeout) {
        super.execute(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()), startTimeout);
    }

    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return super.submit(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    public ListenableFuture<?> submitListenable(Runnable task) {
        return super.submitListenable(task);
    }

    @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
        return super.submitListenable(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    protected void cancelRemainingTask(Runnable task) {
        super.cancelRemainingTask(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    @Override
    public void destroy() {
        super.shutdown();
    }
}
